// pages/todos/todos.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		search: "",
		workInfo: [{
				name: "Learning HTML",
				checked: true
			},
			{
				name: "Learning CSS",
				checked: false
			},
			{
				name: "Learning JavaScript",
				checked: false
			}
		],
		leftCounts: 2,
		checkedAll: false
	},

	/* 搜索框 */
	changeText: function(e) {
		// console.log(e.detail.value)
		if (e.detail.value == " ") {
			wx.showToast({
				title: "首字符不能为空",
				icon: "none",
				duration: 1500
			})
			e.detail.value = "";
		}
		this.setData({
			search: e.detail.value
		})
	},

	/* 添加数据 */
	addListInfo: function(e) {
		// console.log(e.target.dataset.search)
		// 拿到的数据
		if (!this.data.search) {
			return wx.showToast({
				title: "内容不能为空",
				icon: "none",
				duration: 1500
			})
		}
		let search = this.data.search;
		let obj = {
			name: search,
			checked: false
		};
		this.data.workInfo.push(obj)
		this.setData({
			workInfo: this.data.workInfo,
			search: '',
			leftCounts: this.data.leftCounts + 1
		})
	},

	// 改变图标的样式
	changeIconType: function(e) {
		// console.log(e)
		let item = this.data.workInfo[e.currentTarget.dataset.index]
		item.checked = !item.checked
		// 判断当前的状态
		let leftCount = this.data.leftCounts + (item.checked ? -1 : 1);
		this.setData({
			workInfo: this.data.workInfo,
			leftCounts: leftCount
		})
	},

	// 删除本项
	removeThis: function(e) {
		// console.log(e)
		let index = e.currentTarget.dataset.index;
		// console.log(index)
		let item = this.data.workInfo.splice(index, 1)[0];
		console.log(item)
		// 判读当前选中的状态
		let leftCount = this.data.leftCounts - (item.checked ? 0 : 1);
		this.setData({
			workInfo: this.data.workInfo,
			leftCounts: leftCount
		})
	},

	// 全选
	selectAll: function(e) {
		this.data.checkedAll = !this.data.checkedAll
		this.data.workInfo.forEach(item => {
			item.checked = this.data.checkedAll
		})
		let leftCount = this.data.checkedAll ? 0 : this.data.workInfo.length;
		this.setData({
			workInfo: this.data.workInfo,
			leftCounts: leftCount
		})
	},

	// 清空已选中状态的
	deleteAll: function(e) {
		let arr = this.data.workInfo.filter(item=>{
			return !item.checked;
		})
		this.setData({
			workInfo: arr
		})
	}

})
